<?php
/* SVN FILE: $Id: teams_controller.php 250 2010-06-17 10:55:41Z gregory.rome $ */
/**
 * Teams Controller.
 *
 * Teams.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 250 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-17 10:55:41 +0000 (Thu, 17 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Teams Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class TeamsController extends AppController {

    var $name = 'Teams';
    var $paginate = array(
    	'Player' => array(
	    	'contain' => array(
	    		'Skill' => array(
						'LocalizedString',
						'FormattedString'
	  			),
	    		'PlayerType' => array(
	    			'LocalizedString',
	    				'Skill' => array(
								'LocalizedString',
								'FormattedString'
							)
	    		),
	    		'PlayerLevel' => array(
	    			'LocalizedString'),
    )));

    function index() {
      $teams = $this->paginate('Team');

      if (isset($this->params['requested'])) {
          return $teams;
      } else {
          $this->set('teams', $teams);
      }
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Team.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Team->recursive = 2;
		$team = $this->Team->read(null, $id);

		$players = $this->paginate('Player', array('Player.idTeam_Listing'=>$id));
        if (isset($this->params['requested'])) {
            return $team;
        } else {
            $this->set('team', $team);
            $this->set('players', $players);
        }
	}

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Team', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->Team->del($id)) {
            $this->Session->setFlash(__('Team deleted', true));
            $this->redirect(array('action'=>'index'));
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Team', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
        	$this->data['Team']['idCoach'] = $this->data['Team']['Coach'];
        	unset($this->data['Team']['Coach']);
            if ($this->Team->save($this->data)) {
                $this->Session->setFlash(__('The Team has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Team could not be saved. Please, try again.', true));
            }
        }

        $this->data = $this->Team->read(null, $id);
        $teamName = $this->data['Team']['strName'];
        $this->set('teamName', $teamName);

        $coaches = $this->Team->Coach->find('list', array('fields' => array('Coach.ID', 'Coach.strName')));
        $this->set(compact('coaches'));
    }

}
?>
